package observer.deformation;

import java.util.HashMap;
import java.util.Map;

public class SubjectManager {

    private Map<String, Subject> subjectMap = new HashMap<>();

    public void addSubject(Subject subject){
        subjectMap.put(subject.getName(), subject);
    }

    public void addObserver(String subjectName,Observer observer){
        Subject subject = getSubject(subjectName);
        subject.addObserver(observer);
    }

    public void removeObserver(String subjectName,Observer observer){
        Subject subject = getSubject(subjectName);
        subject.removeObserver(observer);
    }

    public Subject getSubject(String name){
        return subjectMap.get(name);
    }

    //单例
    private SubjectManager(){}

    public static SubjectManager getInstance(){
        return SubjectManagerInstance.instance;
    }

    private static class SubjectManagerInstance{
        private static SubjectManager instance = new SubjectManager();
    }
}
